package com.example.service.Impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.AssessmentMapper;
import com.example.pojo.entity.Assessment;
import com.example.pojo.entity.Cost;
import com.example.service.AssessmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@Slf4j
@DS("assessment")
public class AssessmentServiceImpl extends ServiceImpl<AssessmentMapper, Assessment> implements AssessmentService {
    @Autowired
    private AssessmentMapper assessmentMapper;
    @Override
    public void add(Assessment assessment) {
        assessment.setIsDeleted(0);
        assessment.setVersion(0);
        assessmentMapper.insert(assessment);
    }

    /**
     * 分页查询
     */
    public Page<Assessment> selectPage(String name, Integer pageNum, Integer pageSize) {
        Page<Assessment> page = assessmentMapper.selectPage(new Page<Assessment>(pageNum, pageSize),
                new LambdaQueryWrapper<Assessment>()
                        .like(StringUtils.hasLength(name),Assessment::getName,name));
        return page;
    }
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }
    @Override
    public void deleteById(Integer id) {
        Assessment assessment = assessmentMapper.selectById(id);
        assessment.setIsDeleted(1);
        updateAssessment(assessment);
    }
    private void updateAssessment(Assessment assessment) {
        assessmentMapper.updateById(assessment);
    }

    @Override
    public Assessment selectById(Integer id) {
        return assessmentMapper.selectById(id);
    }

    @Override
    public void update(Assessment assessment) {
        assessmentMapper.updateById(assessment);
    }


}
